#pragma once
#include "afxwin.h"
#include "cvxImage.h"
#include "resource.h"
#include "cvxMarker.h"
#include "SideBySideDlg.h"
#include "OpitionsDlg.h"
#include "cvxCamera.h"
#include "OverLay.h"
#include "afxcmn.h"
#include "cvxBoard.h"


// CMechAlignDlg dialog
// Mechnically alignment the stereo camera rig
class CMechAlignDlg : public CDialog
{
	DECLARE_DYNAMIC(CMechAlignDlg)

public:
	CMechAlignDlg(CWnd* pParent = NULL);   // standard constructor
	virtual ~CMechAlignDlg();

// Dialog Data
	enum { IDD = IDD_MECH_ALIGN };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

	DECLARE_MESSAGE_MAP()
public:
	virtual BOOL OnInitDialog();
	afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
	afx_msg void OnPaint();

	//added
	IplImage *m_leftImage;
	IplImage *m_rightImage;
	CSideBySideDlg *m_pSideByside;
	COverLay       *m_pOverlay;

	void SetUpdataDialog(CSideBySideDlg *pDialog){m_pSideByside = pDialog;}
	void SetOverLayDialog(COverLay *pOverlay){m_pOverlay = pOverlay;}
	//added input LUV422 format, 
	void updateImages(char *pdata, char *pRightData, int w, int h, int nChannels, bool flipRight);
	//set camera configure (intrinsic parameters)
	void SetCameraConfigure(const SCameraConfigure &left, const SCameraConfigure &right, int w, int h);
	void EnableCalib(void){m_readyCalib = true;}
	void DisableCalib(void){m_readyCalib = false;}
	void EnableSideBySide(void){m_sideBysideReady = true;}
	void DisableSideBySide(void){m_sideBysideReady = false;}
	void EnableOverlay(void){m_overlayReady = true;}
	void DisableOverlay(void){m_overlayReady = false;}
	void EnableShowIA(void){m_showIAReady = true;}
	void DisableShowIA(void){m_showIAReady = false;}
	void EnableDenseCalib(void){m_denseCalib = true;}
	void DisableDenseCalib(void){m_denseCalib = false;}
	

protected:
	CBrush   m_redBrush;
	CBrush   m_greenBrush;
	COLORREF m_redColor;
	COLORREF m_greenColor;
	LOGFONT  m_font;     //font of alignment parameters

public:
	// convergence angle, in X direction, unit in degree
	CEdit m_convergence;
	// tile angle in Y direction, unit in degree
	CEdit m_tilt;
	// roll angle, angle difference in Z direction, unit in degree
	CEdit m_roll;

private:

	//marker related data
	vector<CvxMarker> m_preLeftMarkers;   //markers not relatec to special chessboard
	vector<CvxMarker> m_preRightMarkers;
	CvxCameraRig m_rig;
	bool m_readyCalib;
	bool m_sideBysideReady;
	bool m_overlayReady;
	bool m_showIAReady; //show inter axis distance ready

	bool m_denseCalib;  //obtain the extrinsic parameters by dense points
	CvxMarkerChessboard m_board;   //marker chessboard used to obtain extrinsic parameters
	vector<CvxMarker> m_boardPeLeftMarkers;   //marker used in the board
	vector<CvxMarker> m_boardPeRightMarkers;
	

	bool trackingMarker();
	void Calib();  //computer camera extrinsic parameter from intrinsic parameter and marker position
	void Align();  //computer zoom level, vertical offset, horizontal offset from marker position.
	void DenseCalib();  //computer camera extrinsic parameter from intrinsic parameter and dense chesboard position
protected:
	// vertical offset percentage 
	CSliderCtrl m_alignVerticalOffsetSlider;
	// vertical offset of alignment from mark position
	CEdit m_alignVerOffsetEdit;
	// horizontal offset , ratio from marker positions
	CSliderCtrl m_alignHorizontalOffsetSlider;
	// horizontal offset edit from ratio of marker positions
	CEdit m_alignHorizontalOffsetEdit;
	// zoom level ratio. 
	CSliderCtrl m_alignZoomSlider;
	// zoom level ratio.
	CEdit m_alignZoomEdit;
	// rotation angle 
	CEdit m_alignRotationEdit;
	// rotation angle
	CSliderCtrl m_alignRotationSlider;
	// keystone, ratio of (l1/l2)/(l1'/l2'). l is height
//	CEdit m_alignKeystoneEdit;
//	CSliderCtrl m_alignKeystoneSlider;
	// inter axis distance
	CEdit m_editInterAxisDistance;
	// re projection error left frame
	CEdit m_projErrLeft;
	// re projection error right
	CEdit m_projErrRight;
};
